package com.ffcs.crmd.tsp.core.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 
 * 功能说明:线程工厂
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class ThreadFactoryImpl implements ThreadFactory {
    private AtomicLong threadIndex = new AtomicLong(0);
    private String     threadNamePrefix;
    private int        priority    = Thread.NORM_PRIORITY;
    
    public ThreadFactoryImpl(final String threadNamePrefix) {
        this.threadNamePrefix = threadNamePrefix;
    }
    
    public ThreadFactoryImpl(final String threadNamePrefix, final int priority) {
        if(priority > 0 && priority <= 10){
            this.priority = priority;
        }
    }
    
    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r, threadNamePrefix + this.threadIndex.incrementAndGet());
        thread.setPriority(priority);
        return thread;
    }
}
